<?php

namespace Dddxiu;

use Dddxiu\Funnel;
use Dddxiu\Message;

use Dddxiu\traits\Singleton;

/**
 * 校验器
 */
class Validator
{
    use Singleton;

    /**
     * @var array
     */
    private $rules;
    /**
     * @var array
     */
    private $error_list;
    /**
     * @var array
     */
    private $input;
    /**
     * @var array
     */
    private $messages;

    /**
     * 自定义校验规则
     * @param string $key 校验器名称
     * @param mixed $call 可调用参数
     * @return void
     */
    protected function make($key, $call, $msg)
    {
        Funnel::register($key, $call);
        Message::register($key, $msg);
    }

    /**
     * 校对信息
     * @param array $input 输入参数
     * @param array $rules 校对规则
     * @param array|boolean $messages 自定义消息|立即返回,传入boolean会忽略$ret
     * @param boolean $interrupt 立即返回
     * @return array|null  校对结果或则错误信息
     * @throws \Exception
     */
    protected function validate($input, $rules, $messages = null, $interrupt = false)
    {
        $this->setInput($input)->setRules($rules);
        if (is_bool($messages)) {
            return $this->check($messages);
        }
        return $this->setMessage($messages)->check($interrupt);
    }

    /**
     * 输入
     */
    protected function setInput($input)
    {
        $this->input = $input;
        return $this;
    }

    /**
     * 规则
     */
    protected function setRules($rules)
    {
        $this->rules = $rules;
        return $this;
    }

    /**
     * 自定义信息
     */
    protected function setMessage($messages = [])
    {
        $this->messages = [];
        foreach ($messages as $field_rule => $message) {
            $field_rule = explode('.', $field_rule);
            if (count($field_rule) !== 2) {
                throw new \Exception("{$field_rule} {$message} format error", 1);
            }
            list($field, $rule) = $field_rule;
            $this->messages[$field][$rule] = $message;
        }
        return $this;
    }

    /**
     * 检查字段:
     *  如果通过,返回校验的字段
     * @param false interrupt 中断
     * @return array|null
     * @throws \Exception
     */
    protected function check($interrupt = false)
    {
        Funnel::standing($this->input, $this->rules, $interrupt);
        $this->error_list = Funnel::getErrors();
        if ($this->pass()) {
            $values = [];
            foreach ($this->rules as $field => $item) {
                if (array_key_exists($field, $this->input)) {
                    $values[$field] = $this->input[$field];
                }
            }
            return $values;
        }
        return null;
    }

    /**
     * 校验通过
     */
    protected function pass()
    {
        return (count($this->error_list) === 0);
    }

    /**
     * 错误列表
     */
    protected function errors($all = false)
    {
        $temp_errors = [];
        foreach ($this->error_list as $field => $rule_list) {
            $msg = $this->messages[$field] ?? [];

            foreach ($rule_list as $rule => $value) {
                list($input_val, $rule_val) = $value;
                $err           = Message::format($rule, [$field, $rule_val], $msg);
                $temp_errors[] = $err;
                if ($all === false) {
                    return $temp_errors;
                }
            }
        }

        return $temp_errors;
    }

    /**
     * 清理错误
     */
    protected function clean()
    {
        $this->rules      = [];
        $this->input      = [];
        $this->messages   = [];
        $this->error_list = [];
    }
}